﻿using FBroSharp.Const;
using FBroSharp.Event;
using System;
using System.IO;

namespace 拦截获取简单实例
{

    public class FBroResponseFilterEvent : FBroSharpResponseFilterEvent
    {
        //用于保存当前处理的资源地址
        public string url = "";

        //用于存放全部数据
        private MemoryStream responseStream;

        private BinaryWriter writer;

        public override void Start(long flag) 
        {
            responseStream = new MemoryStream();
            writer = new BinaryWriter(responseStream);
        }

        //获取结束
        public override void End(long flag)
        {
            byte[] data = responseStream.ToArray();
       
            if (data != null && data.Length > 0)
            {

                string path = Directory.GetCurrentDirectory() + "\\temp\\";

                //不要太长
                if (url.Length > 120)
                    url = url.Substring(0, 120);
            
                //舍却？后面的
                int point = url.IndexOf("?");
                if (point > 0)
                    url = url.Substring(0, point);

                //将链接当作文件名，踢出无法做文件名的符号
                string filename = url.Replace("/", "%");
                filename = filename.Replace(":", ";");

                //判断文件夹是否存在，不存在则重新创建
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                //将数据保存到文件中，当然你也可以不用这么处理
                FileStream fs = new FileStream(path + filename, FileMode.OpenOrCreate);
                fs.Write(data, 0, data.Length);
                fs.Close();
            }

            responseStream = null;
            writer = null;
        }

        public override FBroSharpResponseFilterStatus Filter(long flag, Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
        {
            if (dataIn == null)
            {
                dataInRead = 0;
                dataOutWritten = 0;

                return FBroSharpResponseFilterStatus.RESPONSE_FILTER_DONE;
            }

            //Calculate how much data we can read, in some instances dataIn.Length is
            //greater than dataOut.Length
            dataInRead = Math.Min(dataIn.Length, dataOut.Length);
            dataOutWritten = dataInRead;

            var readBytes = new byte[dataInRead];
            dataIn.Read(readBytes, 0, readBytes.Length);
            dataOut.Write(readBytes, 0, readBytes.Length);

            //Write buffer to the memory stream
            writer.Write(readBytes);

            //If we read less than the total amount available then we need
            //return FilterStatus.NeedMoreData so we can then write the rest
            if (dataInRead < dataIn.Length)
            {
                return FBroSharpResponseFilterStatus.RESPONSE_FILTER_NEED_MORE_DATA;
            }

            return FBroSharpResponseFilterStatus.RESPONSE_FILTER_DONE;
        }


        public override bool InitFilter(long flag)
        {
            //返回真开始拦击
            return true;
        }


    }

}
